package socket;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class Client {
    private Socket socket;

    public Client(){
        try {
            System.out.println("服务器启动...");
            socket = new Socket("176.15.11.72",8088);
            System.out.println("与服务器建立连接");
        } catch (IOException e) {
            e.printStackTrace();
        }}

    public void start() {
        try {
            OutputStream out = socket.getOutputStream();
            System.out.println("请输入内容:");
            while (true) {
                Scanner scanner = new Scanner(System.in);
                String line = scanner.nextLine();
                if("exit".equals(line)){
                    break;
                }
                byte [] data = line.getBytes(StandardCharsets.UTF_8);
                out.write(data.length);
                out.write(data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close();
                System.out.println("客户端退出...");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public static void main(String[] args) {
        Client client = new Client();
        client.start();
    }
}
